<template>
    <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px" class="login-container">
        <h3 class="login_title">系统登录</h3>
        <el-form-item label="用户名" label-width="80px" prop="username" class="username">
            <el-input type="input" v-model="form.username" autocomplete="off" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="80px" prop="password">
            <el-input type="password" v-model="form.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="longin_submit">
            <el-button type="primary" @click="login('form')" class="longin_submit">登录</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    export default {
        data() {
            return {
                form: {
                    username: 'miracle',
                    password: 'miracle'
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {
                            min: 3,
                            message: '用户名长度不能小于3位',
                            trigger: 'blur'
                        }
                    ],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}]
                }
            }
        },
        methods: {
            login(form) {
                var _this = this;
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        _this.$http.post('http://localhost:8001/api/oauth/login', _this.form).then(res => {
                            res = res.data
                            if (res.code === 20000) {
                                //放入token
                                _this.$store.commit('setToken', res.data)

                                _this.$http.get('http://localhost:8001/api/manager/permission/queryPermissionByUser', {
                                    headers: {
                                        "token": res.data
                                    }
                                }).then(ret => {
                                    ret = ret.data
                                    if(ret.code === 20000) {
                                        //先清除 防止重复加入
                                        _this.$store.commit('clearMenu')
                                        //再加入
                                        _this.$store.commit('setMenu', ret.data.permissions)
                                        _this.$store.commit('setUserInfo', ret.data.userInfo)

                                        _this.$store.commit('addMenu', this.$router)

                                        //跳转到首页
                                        _this.$router.push({name: 'home'})
                                    }
                                })
                            } else {
                                //如果失败 提示失败信息
                                _this.$message.warning(res.data.message)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        }
    }
</script>

<style>
    .login-container {
        border-radius: 15px; /*圆角*/
        background-clip: padding-box; /*背景被裁剪到内边距框*/
        margin: 180px auto; /*边界 距上180px 左右居中*/
        width: 350px; /*表单宽度*/
        padding: 35px 35px 15px 35px; /*填充*/
        background: #fff;
        border: 1px solid #eaeaea; /*边框*/
        box-shadow: 0 0 25px #cac6c6; /*隐形*/
    }

    .login_title {
        margin: 0px auto 40px auto; /*上边界0 下边边界40 左右居中*/
        text-align: center; /*上面仅是标签居中 这里是文字居中*/
        color: #505458;
    }

    .longin_submit {
        margin: 10px auto 0px auto; /*上边界10 下边边界40 左右居中*/
    }
</style>
